﻿using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using VANAD.WindowsLive.Writer.InsertFavoritePlugin.UI;

namespace VANAD.WindowsLive.Writer.InsertFavoritePlugin
{
    [
        WriterPlugin("{1EF6D437-6384-4d46-91BE-853A13E56DFB}",
            "Insert Favorite from User Favorites",
            Description = "Let's the user pick and insert a title and URL from the users IE Favorites.",
            ImagePath = "Images.favorite-icon.jpg",
            PublisherUrl = "http://www.vanad.nl/"),
        InsertableContentSource("from Favorites")
    ]
    public class InsertFavoritePlugin : ContentSource
    {
        private string _lastSelectedNodeName;

        public InsertFavoritePlugin()
        {
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
        {
            DialogResult dr = DialogResult.Cancel;
            SelectFavoriteDialogForm form = new SelectFavoriteDialogForm(_lastSelectedNodeName);

            try
            {
                dr = form.ShowDialog();

                if (dr == DialogResult.Cancel)
                    return dr;

                if (form.LastSelectedNode != null)
                    _lastSelectedNodeName = form.LastSelectedNode.Name;
                
                content = form.FavoriteToInsert.HTMLInsertText;
            }
            catch (ArithmeticException ex)
            { }
            finally
            {
                form.Dispose();
            }

            return dr;
        }
    }
}
